﻿/*
Copyright (c) 2010  
*/

function getDialog(name){
	var $dlg = $(name);
	if($dlg.length<=0) {
		$dlg = $("<div></div>").attr('id',name.substr(1)).css('display','none').appendTo($(document.body));
	}
	return $dlg;
}

function showMessage(msg,caption,callback)
{
	if(caption==null) caption='CIOA';
	getDialog("#_dialog-panel").html(msg).dialog({
		resizable: false,
		modal: true,
		title:caption,
		buttons: {			
			'确定': function() {
				$(this).dialog('close');
				if(callback) callback();
			}
		}
	});
}

function askYesOrNo(option)
{
	option=$.extend({
		title:'CIOA',
		OKText:'确定',
		CancelText:'取消',
		onok:function($dialog){},
		oncancel:function($dialog){}
	},option);
	getDialog("#_dialog-panel").html(option.message).dialog({
		resizable: false,
		modal: true,
		title:option.title,
		buttons: {
			'取消': function() {
		        $(this).dialog('close');
		        option.oncancel($(this));		        
			},
			'确定': function() {
				$(this).dialog('close');
				option.onok($(this));
			}
		}
	});
}

function showForm(caption,url,w,h)
{
	var $form = getDialog("#_form-panel");
	$form.html('').load(url).dialog({
		resizable: true,
		modal: true,
		title:caption,
		width: w?w:300,
		height: h?h:'auto'
	});
}

function closeForm()
{
	getDialog("#_form-panel").dialog("close");
}

function refreshPage(formName)
{
	var $form = null;
	
	if(!formName) $form = $(document.forms[0]);
	else if ( typeof formName === "string" ) {		
		if(formName.length>0 && formName.charAt(0)!='#') formName='#'+formName;
		$form=$(formName);		
	}else if(formName.nodeType){
		$form=$(formName);
	}else if(formName.context && formName.onevent){
		$form = formName;
	}
	
	if($form!=null){
		$form.onevent('#onrefresh');
	}
}

function firstChecked(name){
	var $items = $('input:checked[name='+name+']');
	if($items.length <= 0) return null;
	else return $items[0].value;
}

function onSave(listenerName,eventName,params){
	  if(listenerName != null){
	  	  var listener = document.getElementById(listenerName);
	      if(listener != null){
	      	  if(!listener.hasChanged){
	      	  	  alert("您还没有修改任何数据！");
	      	  	  return;
	      	  }
	      	  listener.hidenPrompt=true;
	      }
	  }	  
	  on_event(eventName,params);
}

function processUrl(url){
	  var ran = Math.random()*100000;
	  ran=Math.round(ran);
	  
	  var index=url.indexOf("?");
	  var newUrl;
	  
	  if(index>=0){
	  	  var newUrl = url.substr(0,index);
	  	  var tmp = url.substr(index+1);
	  	  index=tmp.indexOf("?");
	  	  if(index>=0){
	  	  	  var s1 = tmp.substr(0,index);
	  	  	 	  	  	  
	  	  	  s1=s1+"?mran="+ran+"&";
	  	  	  s1=s1+tmp.substr(index+1);
	  	  	  tmp=encodeUrl(s1);
	  	  }else if( (index=tmp.indexOf("preurl=")) >=0 ){	  	  	
	  	  	  tmp=tmp+"?mran="+ran;
	  	  	  tmp=encodeUrl(tmp);
	  	  }
	      newUrl=newUrl+"?mran="+ran+"&"+tmp;
	  }else{
	  	 newUrl=url+"?mran="+ran;
	  }
	  
	  return newUrl;
}

function ajaxSubmit($form,onResponse){
	$form.ajaxSubmit({
		dataType:'json',
		timeout: 20000,
		complete: function( res, status ){
		    //alert( res.responseText );
	    },
		success:function(json) {
           if(json!=null && json.length==1){
        	   if(json[0].title=="expire" && json[0].message=="relogin")
        	   {
            	   window.location.href="../login.jsp";
            	   return;
        	   }
        	   
        	   if(onResponse) {
    			   if(!onResponse(json[0].result,json[0])) return;
    		   }
        	   
        	   showMessage(json[0].message,json[0].title);
           }else{
        	   showMessage('服务器响应错误！');
           }
        },
        error:function(){
        	if(onResponse) onResponse(-1);
        	else showMessage('提交请求出错！');
        }
	}); 
}

(function($) {
	$.extend($.fn, {
		onevent:function(name,args,callback){
		    if(!this.length) return;
		    var $theForm = $(this[0]);
		    
		    var $nameField = $theForm.find('#__EVENTTARGET');
		    if(!$nameField.length){
		    	$nameField=$("<input type='hidden' id='__EVENTTARGET'>").appendTo($theForm);
		    }
		    $nameField.val(name);
		    
		    if(args!=null && args!=''){
		    	var $argField = $theForm.find('#__EVENTARGUMENT');
		    	if(!$argField.length){
		    		$argField=$("<input type='hidden' id='__EVENTARGUMENT'>").appendTo($theForm);
		    	}
		    	$argField.val(args);
		    }
		    
		    if(callback) ajaxSubmit($theForm,callback);
		    else $theForm.submit();		    
		    return $(this);
	    }
	});
	
})(jQuery);

function openDialog(surl,winName,w,h,x,y){
	   if(w == null) w = 300;
	   if(h == null) h = 300;
	   if(x == null) x = 200;
	   if(y == null) y = 100;
	   
	   //if(winName==null) winName="_blank";
	   if(winName==null) winName="_blank";
	   
	   var win = null;
	   try{
	       win = window.open(surl,winName,
	           'height='+h+',width='+w+',top='+y+',left='+x+',location=no,resizable=yes,scrollbars,status=no,toolbar=no');
	   }catch(e){win=null;}
	   
	   if(win == null) return false;
	   else return true;
}

//打开excel文档
var excelTimer;

function openExcel(fileUrl)
{		
	if($.browser.msie)
	{
		var xls = null;
		  
	    try 
	    {
	         xls    = new ActiveXObject ( "Excel.Application" );
	    }
	    catch(e) 
	    {
	         alert( "要打印该表，您必须安装Excel电子表格软件，同时浏览器须使用“ActiveX 控件”，您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法！");
	         return null;
	    }
	    
	    xls.visible =true;  //设置excel为可见
	    if(fileUrl != null)
	    {
		      xls.Workbooks.Open(fileUrl,null,true);
		}
	    
	    try
	    {
	    	//调整
	    	var xsheet = xls.Workbooks(1).ActiveSheet;
	    	var r_count = xsheet.UsedRange.Rows.Count;
	    	var c_count = xsheet.UsedRange.Columns.Count;
	    	for(var i=1;i<=r_count;i++)
	    	{
	    	    for(var j=1;j<=c_count;j++)
	    	    {
	    	        var r = xsheet.UsedRange.Cells(i, j);
	    	        if(String(r) == 'undefined' || typeof(r.Value)!='string') continue;
	    	        
	    	        //alert(r.MergeCells+'----'+r.Value);
	    	        if(r.MergeCells && r.Value
	    	        	&& r.Value!='' && r.Value!=null)
	    	        {
	    	            var str=r.Value.split("\n");
	    	            if(str.length>1)
	    	            {
	    	                xsheet.UsedRange.Rows(i).RowHeight = 14.25*str.length;
	    	            }
	    	        }
	    	    }
	    	}
	    }
	    catch(e){}
	    
	    //xls.ActiveSheet.Protect( "ciPay123",true,true,true);
		//xls.DisplayAlerts = false;
		  
		//防止excel进程死掉
		excelTimer = window.setInterval("Cleanup();",1000);
		return xls;
	}
	else
	{
		/*非IE，直接下载*/
		window.open(fileUrl,'_blank');
	}
	
}

function editExcel(fileUrl)
{		
	if($.browser.msie)
	{
		var xls = null;
		  
	    try 
	    {
	         xls    = new ActiveXObject ( "Excel.Application" );
	    }
	    catch(e) 
	    {
	         alert( "要打印该表，您必须安装Excel电子表格软件，同时浏览器须使用“ActiveX 控件”，您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法！");
	         return null;
	    }
	    
	    xls.visible =true;  //设置excel为可见
	    if(fileUrl != null)
	    {
		      xls.Workbooks.Open(fileUrl,null,true);
		}
	    
	    try
	    {
	    	//调整
	    	var xsheet = xls.Workbooks(1).ActiveSheet;
	    	var r_count = xsheet.UsedRange.Rows.Count;
	    	var c_count = xsheet.UsedRange.Columns.Count;
	    	for(var i=1;i<=r_count;i++)
	    	{
	    	    for(var j=1;j<=c_count;j++)
	    	    {
	    	        var r = xsheet.UsedRange.Cells(i, j);
	    	        if(String(r) == 'undefined' || typeof(r.Value)!='string') continue;
	    	        
	    	        //alert(r.MergeCells+'----'+r.Value);
	    	        if(r.MergeCells && r.Value
	    	        	&& r.Value!='' && r.Value!=null)
	    	        {
	    	            var str=r.Value.split("\n");
	    	            if(str.length>1)
	    	            {
	    	                xsheet.UsedRange.Rows(i).RowHeight = 14.25*str.length;
	    	            }
	    	        }
	    	    }
	    	}
	    }
	    catch(e){}
	    
	   
		xls.DisplayAlerts = false;
		  
		//防止excel进程死掉
		excelTimer = window.setInterval("Cleanup();",1000);
		return xls;
	}
	else
	{
		/*非IE，直接下载*/
		window.open(fileUrl,'_blank');
	}
	
}

function Cleanup() 
{
   	window.clearInterval(excelTimer);
    CollectGarbage();
}
